import { reactive, computed } from "vue";
import { message } from "@/utils/message";
import type { FormItemConfig } from "@/components/DynamicForm/types";

export function useTaskForm() {
  // 表单数据
  const searchFormData = reactive({
    search: "",
  });

  const addTaskFormData = reactive({
    name: "",
    target: "",
    ignore: "",
    template: "",
    scheduledTasks: false, // 定时任务开关
    cycleType: "", // 监控周期类型
    hour: 0, // 小时
    minute: 0, // 分钟
    day: 1, // 天数或日期
    week: 0, // 星期几
    deduplicate: false, // 去重
    subdomainDeduplicate: false, // 子域名去重
    scanTemplate: "", // 扫描模板
  });

  // 星期选项
  const weekOptions = [
    { label: "星期日", value: 0 },
    { label: "星期一", value: 1 },
    { label: "星期二", value: 2 },
    { label: "星期三", value: 3 },
    { label: "星期四", value: 4 },
    { label: "星期五", value: 5 },
    { label: "星期六", value: 6 },
  ];

  // 搜索表单配置
  const searchFormConfig: FormItemConfig[] = [
    {
      key: "search",
      name: "任务名称",
      type: "input",
      placeholder: "任务名称",
      params: {
        maxLength: 30,
        clearable: true,
      },
      col: { xs: 24, sm: 12, md: 8, lg: 6 },
    },
    // {
    //   key: "creater",
    //   name: "创建人",
    //   type: "input",
    //   placeholder: "创建人",
    //   params: {
    //     maxLength: 30,
    //     clearable: true
    //   },
    //   col: { xs: 24, sm: 12, md: 8, lg: 6 }
    // },
    // {
    //   key: "ipscope",
    //   name: "目标",
    //   type: "input",
    //   placeholder: "目标",
    //   params: {
    //     maxLength: 30,
    //     clearable: true
    //   },
    //   col: { xs: 24, sm: 12, md: 8, lg: 6 }
    // }
  ];

  // 新建任务表单配置
  const addTaskFormConfig = computed<FormItemConfig[]>(() => [
    {
      key: "name",
      name: "任务名称",
      type: "input",
      required: true,
      placeholder: "请输入任务名称",
      params: {
        maxLength: 50,
        clearable: true,
      },
    },
    {
      key: "target",
      name: "扫描目标",
      type: "textarea",
      required: true,
      placeholder:
        "请输入扫描目标，如：192.168.1.1-254、www.baidu.com，多个目标用换行或逗号分隔",
      params: {
        rows: 4,
        clearable: true,
      },
    },
    {
      key: "ignore",
      name: "忽略目标",
      type: "textarea",
      placeholder: "请输入需要忽略的目标，多个目标用换行或逗号分隔",
      params: {
        rows: 3,
        clearable: true,
      },
    },

    {
      key: "template",
      name: "扫描类型",
      type: "select",
      required: true,
      placeholder: "请选择扫描类型",
      options: [
        {
          label: "网管扫描",
          value: "684db8adb02f85e29536d9e8",
        },
        {
          label: "互联网探测",
          value: "684db9a5b02f85e29536da04",
        },
        {
          label: "探测与漏扫",
          value: "684eac94b02f85e29536e070",
        },
        {
          label: "深度扫描",
          value: "684f1336b02f85e29536e1f9",
        },
      ],
      params: {
        clearable: true,
      },
    },
    {
      key: "scheduledTasks",
      name: "定时任务",
      type: "switch",
    },
    {
      key: "cycleType",
      name: "监控周期",
      type: "select",
      show: (formData) => formData.scheduledTasks,
      required: true,
      placeholder: "请选择监控周期",
      options: [
        { label: "每天", value: "daily" },
        { label: "N天", value: "ndays" },
        { label: "N小时", value: "nhours" },
        { label: "每星期", value: "weekly" },
        { label: "每月", value: "monthly" },
      ],
      params: {
        clearable: true,
      },
    },
    // daily: hour + minute
    {
      key: "hour",
      name: "小时",
      type: "number",
      show: (formData) =>
        formData.scheduledTasks &&
        (formData.cycleType === "daily" ||
          formData.cycleType === "ndays" ||
          formData.cycleType === "weekly" ||
          formData.cycleType === "monthly"),
      required: true,
      defaultValue: 0,
      params: {
        min: 0,
        max: 23,
        step: 1,
      },
    },
    {
      key: "minute",
      name: "分钟",
      type: "number",
      show: (formData) => formData.scheduledTasks && formData.cycleType,
      required: true,
      defaultValue: 0,
      params: {
        min: 0,
        max: 59,
        step: 1,
      },
    },
    // ndays: day + hour + minute
    {
      key: "day",
      name: "间隔天数",
      type: "number",
      show: (formData) =>
        formData.scheduledTasks && formData.cycleType === "ndays",
      required: true,
      defaultValue: 1,
      params: {
        min: 1,
        max: 365,
        step: 1,
      },
    },
    // nhours: hour + minute (hour 表示间隔小时数)
    {
      key: "hour",
      name: "间隔小时数",
      type: "number",
      show: (formData) =>
        formData.scheduledTasks && formData.cycleType === "nhours",
      required: true,
      defaultValue: 1,
      params: {
        min: 1,
        max: 168, // 一周的小时数
        step: 1,
      },
    },
    // weekly: week + hour + minute
    {
      key: "week",
      name: "星期几",
      type: "select",
      show: (formData) =>
        formData.scheduledTasks && formData.cycleType === "weekly",
      required: true,
      placeholder: "请选择星期几",
      options: weekOptions,
      params: {
        clearable: true,
      },
    },
    // monthly: day + hour + minute
    {
      key: "day",
      name: "日期",
      type: "number",
      show: (formData) =>
        formData.scheduledTasks && formData.cycleType === "monthly",
      required: true,
      defaultValue: 1,
      params: {
        min: 1,
        max: 31,
        step: 1,
      },
    },
  ]);

  // 表单验证逻辑
  const validateTaskForm = () => {
    if (!addTaskFormData.name.trim()) {
      message("请输入任务名称", { type: "error" });
      return false;
    }

    if (!addTaskFormData.target.trim()) {
      message("请输入扫描目标", { type: "error" });
      return false;
    }

    if (!addTaskFormData.template) {
      message("请选择扫描类型", { type: "error" });
      return false;
    }

    if (!addTaskFormData.scanTemplate) {
      message("请选择扫描模板", { type: "error" });
      return false;
    }

    // 定时任务开启时的验证
    if (addTaskFormData.scheduledTasks) {
      if (!addTaskFormData.cycleType) {
        message("请选择监控周期", { type: "error" });
        return false;
      }

      // 验证分钟字段
      if (addTaskFormData.minute < 0 || addTaskFormData.minute > 59) {
        message("请设置正确的分钟数(0-59)", { type: "error" });
        return false;
      }

      // 根据不同的周期类型进行相应验证
      switch (addTaskFormData.cycleType) {
        case "daily":
          if (addTaskFormData.hour < 0 || addTaskFormData.hour > 23) {
            message("请设置正确的小时数(0-23)", { type: "error" });
            return false;
          }
          break;

        case "ndays":
          if (!addTaskFormData.day || addTaskFormData.day < 1) {
            message("请设置正确的间隔天数", { type: "error" });
            return false;
          }
          if (addTaskFormData.hour < 0 || addTaskFormData.hour > 23) {
            message("请设置正确的小时数(0-23)", { type: "error" });
            return false;
          }
          break;

        case "nhours":
          if (!addTaskFormData.hour || addTaskFormData.hour < 1) {
            message("请设置正确的间隔小时数", { type: "error" });
            return false;
          }
          break;

        case "weekly":
          if (addTaskFormData.week < 0 || addTaskFormData.week > 6) {
            message("请选择正确的星期", { type: "error" });
            return false;
          }
          if (addTaskFormData.hour < 0 || addTaskFormData.hour > 23) {
            message("请设置正确的小时数(0-23)", { type: "error" });
            return false;
          }
          break;

        case "monthly":
          if (
            !addTaskFormData.day ||
            addTaskFormData.day < 1 ||
            addTaskFormData.day > 31
          ) {
            message("请设置正确的日期(1-31)", { type: "error" });
            return false;
          }
          if (addTaskFormData.hour < 0 || addTaskFormData.hour > 23) {
            message("请设置正确的小时数(0-23)", { type: "error" });
            return false;
          }
          break;
      }
    }

    return true;
  };

  // 重置表单
  const resetAddTaskForm = () => {
    Object.assign(addTaskFormData, {
      name: "",
      target: "",
      ignore: "",
      template: "",
      scheduledTasks: false,
      cycleType: "",
      hour: 0,
      minute: 0,
      day: 1,
      week: 0,
      deduplicate: false,
      subdomainDeduplicate: false,
      scanTemplate: "",
    });
  };

  // 获取搜索参数
  const getSearchParams = () => {
    return searchFormData.search;
  };

  return {
    // 数据
    searchFormData,
    addTaskFormData,
    weekOptions,

    // 配置
    searchFormConfig,
    addTaskFormConfig,

    // 方法
    validateTaskForm,
    resetAddTaskForm,
    getSearchParams,
  };
}
